package cn.school.demo.ui.party_history_answer

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.school.demo.MyApplication
import cn.school.demo.R
import cn.school.demo.databinding.FragmentProfessionalBinding
import cn.school.demo.pojo.Notice
import cn.school.demo.pojo.Subject
import cn.school.demo.repository.AnswerRepository
import cn.school.demo.ui.notice.NoticeFragment
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import java.time.LocalDateTime


class ProfessionalFragment : Fragment() {

    private lateinit var viewmodel: ProfessionalViewModel
    private lateinit var binding: FragmentProfessionalBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        MyApplication.systemBarSetting(MyApplication.DARK, requireActivity())
        viewmodel = ViewModelProvider(this).get(ProfessionalViewModel::class.java)
        binding =
            DataBindingUtil.inflate(inflater, R.layout.fragment_professional, container, false)
        binding.lifecycleOwner = viewLifecycleOwner
        binding.vw = this
        viewmodel.professions.observe(viewLifecycleOwner) {
            initUI()
        }
        return binding.root
    }

    private fun initUI() {
        binding.fragmentProfessionalRecyclerView.layoutManager =
            LinearLayoutManager(requireContext())
        val professions = viewmodel.professions.value!!
        val adapter = ProfessionalAdapter(professions)
        binding.fragmentProfessionalRecyclerView.adapter = adapter
    }


    private inner class ProfessionalHolder(view: View) : RecyclerView.ViewHolder(view),
        View.OnClickListener {
        private lateinit var professionalTitle: String
        private val titleTextView =
            itemView.findViewById<TextView>(R.id.fragment_professional_recyclerView_item_title)
        private val button =
            itemView.findViewById<Button>(R.id.fragment_professional_recyclerView_item_button)

        init {
            itemView.setOnClickListener(this)
        }

        @SuppressLint("NewApi")
        fun bind(title: String) {
            this.professionalTitle = title
            titleTextView.text = this.professionalTitle
            button.setOnClickListener {
                //向服务器发送请求，获得题目List，参数为title
                viewmodel.apply {
                    getTestList(title, parentFragmentManager)
                    testList.observe(viewLifecycleOwner) {
                        LogUtils.d("我监听到了，我要出发了！")
                        val map = mapOf("title" to "专项答题", "introduction" to "术业有专攻，道义有精论！")
                        val bundle: Bundle = bundleOf(Pair("map", GsonUtils.toJson(map)))
                        MyApplication.apply {
                            testList = viewmodel.testList.value!!
                            subjectList.apply {
                                clear()
                                add(
                                    Subject(
                                        1,
                                        1,
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        "",
                                        LocalDateTime.now().toString(),
                                        LocalDateTime.now().toString(),
                                        1
                                    )
                                )
                            }
                            navigateThing["AnswerMain"] =
                                R.id.action_AnswerMain_to_Professional.toString()
                            navigateThing["Answer"] = R.id.action_Answer_to_Professional.toString()
                            isSubject = false
                            goto(
                                R.id.action_Professional_to_AnswerEntry,
                                findNavController(),
                                requireFragmentManager(),
                                bundle
                            )
                        }
                    }
                }
            }
        }

        override fun onClick(v: View) {
            LogUtils.d("无效果！")
        }
    }

    private inner class ProfessionalAdapter(var professionalTitles: MutableList<String>) :
        RecyclerView.Adapter<ProfessionalHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProfessionalHolder {
            val view = layoutInflater.inflate(R.layout.list_item_professional, parent, false)
            return ProfessionalHolder(view)
        }

        override fun onBindViewHolder(holder: ProfessionalHolder, position: Int) {
            val professionalTitle = professionalTitles[position]
            holder.bind(professionalTitle)
        }

        override fun getItemCount() = professionalTitles.size

    }

    fun gotoBack() = MyApplication.goto(
        R.id.action_Professional_to_PartyHistoryAnswer,
        findNavController(),
        requireFragmentManager()
    )
}